package com.jqsoft.launcher.util;

import java.io.PrintWriter;
import java.io.StringWriter;

public final class Log {
	/**
	 * 日志是否有效,true:打印日志,false:不打印日志.
	 */
	public static boolean enable = true;
	/**
	 * 打印日志的级别
	 */
	public static boolean enableVerbose = true;
	public static boolean enableDebug = true;
	public static boolean enableInfo = true;
	public static boolean enableWarn = true;
	public static boolean enableError = true;

	/**
	 * Priority constant for the println method; use Log.v.
	 */
	public static final int VERBOSE = 2;

	/**
	 * Priority constant for the println method; use Log.d.
	 */
	public static final int DEBUG = 3;

	/**
	 * Priority constant for the println method; use Log.i.
	 */
	public static final int INFO = 4;

	/**
	 * Priority constant for the println method; use Log.w.
	 */
	public static final int WARN = 5;

	public static final int ERROR = 6;

	public static final int ASSERT = 7;

	private Log() {
	}

	// public static void v(String tag, String msg) {
	// }
	//
	// public static void v(String tag, String msg, Throwable tr) {
	// }
	//
	// public static void d(String tag, String msg) {
	// }
	//
	// public static void d(String tag, String msg, Throwable tr) {
	// }
	//
	// public static void i(String tag, String msg) {
	// }
	//
	// public static void i(String tag, String msg, Throwable tr) {
	// }
	//
	// public static void w(String tag, String msg) {
	// }
	//
	// public static void w(String tag, String msg, Throwable tr) {
	// }
	//
	// public static void w(String tag, Throwable tr) {
	// }
	//
	// public static void e(String tag, String msg) {
	// }
	//
	// public static void e(String tag, String msg, Throwable tr) {
	// }

	public static void v(String tag, Throwable tr) {
		v(tag, getStackTraceString(tr));
	}

	public static void d(String tag, Throwable tr) {
		d(tag, getStackTraceString(tr));
	}

	public static void i(String tag, Throwable tr) {
		i(tag, getStackTraceString(tr));
	}

	public static void w(String tag, Throwable tr) {
		w(tag, getStackTraceString(tr));
	}

	public static void e(String tag, Throwable tr) {
		e(tag, getStackTraceString(tr));
	}

	public static void v(String tag, String msg) {
		if (!enableVerbose)
			return;
		print(VERBOSE, tag, msg);
	}

	public static void v(String tag, String msg, Throwable tr) {
		if (!enableVerbose)
			return;
		print(VERBOSE, tag, msg, tr);
	}

	public static void d(String tag, String msg) {
		if (!enableDebug)
			return;
		print(DEBUG, tag, msg);
	}

	public static void d(String tag, String msg, Throwable tr) {
		if (!enableDebug)
			return;
		print(DEBUG, tag, msg, tr);
	}

	public static void i(String tag, String msg) {
		if (!enableInfo)
			return;
		print(INFO, tag, msg);
	}

	public static void i(String tag, String msg, Throwable tr) {
		if (!enableInfo)
			return;
		print(INFO, tag, msg, tr);
	}

	public static void w(String tag, String msg) {
		if (!enableWarn)
			return;
		print(WARN, tag, msg);
	}

	public static void w(String tag, String msg, Throwable tr) {
		if (!enableWarn)
			return;
		print(WARN, tag, msg, tr);
	}

	public static void e(String tag, String msg) {
		if (!enableError)
			return;
		print(ERROR, tag, msg);
	}

	public static void e(String tag, String msg, Throwable tr) {
		if (!enableError)
			return;
		print(ERROR, tag, msg, tr);
	}

	private static String getStackTraceString(Throwable tr) {
		if (tr == null) {
			return "";
		}
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		tr.printStackTrace(pw);
		return sw.toString();
	}

	private static void print(int priority, String tag, String msg, Throwable tr) {
		if (!enable)
			return;
		print(priority, tag, msg);
		print(priority, tag, getStackTraceString(tr));
	}

	private static void print(int priority, String tag, String msg) {
		if (!enable)
			return;
		android.util.Log.println(priority, tag, new StringBuffer("[jingqi]")
				.append(msg).toString());
	}
}
